package carltechnology.com.mahjonggame.utils.dao.impl;

import android.content.ContentValues;
import android.util.Log;

import carltechnology.com.mahjonggame.mode.IData;
import carltechnology.com.mahjonggame.utils.Utils;
import carltechnology.com.mahjonggame.utils.dao.IMahjongDao;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by WZTCM on 2016/6/13.
 */
public class MahjongGameInfoStoreHelper {
    private static final String TAG = MahjongGameInfoStoreHelper.class.getSimpleName();
    private IMahjongDao iMahjongDao;
    private Subscription subscription;
    private Subscriber<IData> subscriberInfoData;

    public MahjongGameInfoStoreHelper(IMahjongDao iMahjongDao) {
        this.iMahjongDao = iMahjongDao;
        subscription = null;
        subscriberInfoData = null;
    }

    public void beginStore() {
        subscription = getStoreDataObservable();
        Log.d(TAG, "begin store info");
    }

    public void endStore() {
        if (subscription == null)
            return;
        subscription.unsubscribe();
        Log.d(TAG, "end store info");
        subscription = null;
        subscriberInfoData = null;
    }

    public void storeMahjongInfo(IData iData) {
        if (subscription == null) {
            beginStore();
        }
        if (subscriberInfoData!=null){
            subscriberInfoData.onNext(iData);
            Log.d(TAG,"Store Successfully");
        }
    }


    // main function for store info
    private Subscription getStoreDataObservable() {
        Observable<ContentValues> observable = Observable.create(new Observable.OnSubscribe<IData>() {
            @Override
            public void call(Subscriber<? super IData> subscriber) {
                // subscriber.onNext(data);
                subscriberInfoData = (Subscriber<IData>) subscriber;
            }
        })
                .onBackpressureBuffer(Utils.CONSTANT.ONBACKBUFFER_SMALL_SIZE.getConstant())
                .subscribeOn(Schedulers.computation())
                .observeOn(Schedulers.computation())
                .map(new Func1<IData, ContentValues>() {
                    @Override
                    public ContentValues call(IData data) {
                        ContentValues contentValues = new ContentValues();
                        String[] names = data.getName();
                        StringBuilder sb = new StringBuilder(names[1])
                                .append("_").append(names[1])
                                .append("_").append(names[2])
                                .append("_").append(names[3]);
                        contentValues.put("name",
                                sb.toString());
                        contentValues.put("date",
                                data.getDate());

                        return contentValues;
                    }
                })
                .observeOn(Schedulers.io());
        return iMahjongDao.storeMahjongGameInfo(observable);
    }


}
